package com.uchengwang.ui.cm_ui.me.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.UserConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.me.adapter.TranferAdapter;
import com.uchengwang.ui.cm_ui.ucity.cityBean.UserBeanList;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2018/4/24.
 * 社团账号 转让界面
 */

public class TranferActivity extends BaseWithTitleActivity{
    private EditText tranfer_edit;
    private RecyclerView tranfer_rv;
    private TranferAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_tranfer);
        setTitle("转让管理权");

        initView();
    }

    private void initView() {
        tranfer_edit = (EditText) findViewById(R.id.tranfer_edit);
        tranfer_rv = (RecyclerView) findViewById(R.id.tranfer_rv);
        tranfer_edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                String k = tranfer_edit.getText().toString();
                if (TextUtils.isEmpty(k)){
                    return true;
                }
                UCommonUtil.getUtil().hideSoftInput(TranferActivity.this, getRy());
                getFriends(k);
                return true;
            }
        });

        initData();
    }

    private void initData(){
        adapter = new TranferAdapter(this);
        tranfer_rv.setLayoutManager(new LinearLayoutManager(this));
        tranfer_rv.setAdapter(adapter);
    }

    //请求网络
    private void getFriends(String key){
        MapRequest map =new MapRequest();
        if (!TextUtils.isEmpty(key)){
            map.put(BaseConfig.KEY_WORD, key);
        }
        map.put(BaseConfig.ROLE, BaseConfig.STUDENT_ROLE);

        map.put(BaseConfig.PAGE, 1);
        map.put(BaseConfig.PERPAGE, 20);

        HttpManager.newInstance(this).query(UserConfig.ChangeInfo.method_check, UserConfig.ChangeInfo.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "status="+status+"=msg="+msg);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status="+status+"=msg="+msg);

                if (status != 0){
                    adapter.getData().clear();
                    adapter.notifyDataSetChanged();
                    UCommonUtil.getUtil().showToast(TranferActivity.this, msg);
                    return;
                }
                if (!TextUtils.isEmpty(data) && !data.equals("[]")){
                    UserBeanList listBean = JsonUtils.getManager().stringToBean(data, UserBeanList.class);
                    List<UserBean> list = listBean.getData();
                    if (list != null && list.size() >0){
                        adapter.setData(list);
                    }
                }
            }
        });
    }
}
